package com.jgp.security.shiro;

import com.jgp.security.pojo.UserInfo;
import com.jgp.security.shiro.pojo.WxOpenIdToken;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;

/**
 * 项目   parent
 * 作者   loufei
 * 时间   2018/4/6
 */
public class Md5Matcher extends HashedCredentialsMatcher {
    
    private boolean devMode;

    public Md5Matcher(boolean devMode) {
        super("md5");
        this.devMode = devMode;
    }

    @Override
    public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
        UserInfo userInfo = (UserInfo)info.getPrincipals().getPrimaryPrincipal();
        this.setHashIterations(userInfo.getEncryptTime());
        if(this.devMode){
            return true;
        }else if(token.getClass().equals(WxOpenIdToken.class)){
            if(((WxOpenIdToken) token).getOpenId().equals(userInfo.getOpenId())){
                return true;
            }else{
                return false;
            }
        }else{
            return super.doCredentialsMatch(token,info);
        }
    }

}
